package com.qf.fmcommodity.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qf.fmcommodity.user.entity.Product;
import com.qf.fmcommodity.user.entity.ProductImg;
import com.qf.fmcommodity.user.entity.ProductSku;
import com.qf.fmcommodity.user.entity.ShoppingCart;
import com.qf.fmcommodity.user.mapper.ShoppingCartMapper;
import com.qf.fmcommodity.user.service.IProductImgService;
import com.qf.fmcommodity.user.service.IProductService;
import com.qf.fmcommodity.user.service.IProductSkuService;
import com.qf.fmcommodity.user.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车  服务实现类
 * </p>
 *
 * @author 苇名一心
 * @since 2023-02-13
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    @Autowired
    IProductService productService;

    @Autowired
    IProductSkuService productSkuService;

    @Autowired
    IProductImgService productImgService;

    @Override
    public List<ShoppingCart> listShopcart(Integer userId) {

        //获取userId对应的数据,查询数据库
        List<ShoppingCart> shoppingCartList = list(new QueryWrapper<ShoppingCart>().eq("user_id", userId));


        //填充缺失的信息
        populate(shoppingCartList);

        return shoppingCartList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Integer cid, Integer sum) {

        update(new UpdateWrapper<ShoppingCart>().set("cart_num",sum).eq("cart_id", cid));

    }

    @Override
    public List<ShoppingCart> listbycids(String[] cids) {

        List<ShoppingCart> shoppingCarts = list(new QueryWrapper<ShoppingCart>().in("cart_id", cids));

        populate(shoppingCarts);
        return shoppingCarts;
    }


    public void populate(List<ShoppingCart> shoppingCartList) {

        //List<ShoppingCart> ----> list<producId>
        List<String> producIds = shoppingCartList.stream().map(shoppingCart -> shoppingCart.getProductId()).collect(Collectors.toList());

        List<String> skuids = shoppingCartList.stream().map(shoppingCart -> shoppingCart.getSkuId()).collect(Collectors.toList());

        //查询所有相关商品信息
        List<Product> productList = productService.list(new QueryWrapper<Product>().in("product_id", producIds));

        //List<product> ---> Map<productId,product>
        Map<String, Product> productMap = productList.stream().collect(Collectors.toMap(product -> product.getProductId(), product -> product));



        List<ProductSku> productSkus = productSkuService.list(new QueryWrapper<ProductSku>().in("sku_id", skuids));

        //List<productSku> ---> Map<productId,productSku>
        Map<String, ProductSku> productSkuMap = productSkus.stream().collect(Collectors.toMap(productSku -> productSku.getSkuId(), productSku -> productSku));


        List<ProductImg> productImgs = productImgService.list(new QueryWrapper<ProductImg>().in("item_id", producIds).eq("is_main",1));

        Map<String, ProductImg> imgMap = productImgs.stream().collect(Collectors.toMap(productImg -> productImg.getItemId(), productImg -> productImg));

        //遍历，填充缺失的信息
        for (ShoppingCart shoppingCart : shoppingCartList) {
            String productId = shoppingCart.getProductId();
            String skuId = shoppingCart.getSkuId();
            Product product = productMap.get(productId);
            ProductSku productSku = productSkuMap.get(skuId);
            ProductImg productImg = imgMap.get(productId);

            shoppingCart.setProductName(product.getProductName());
            shoppingCart.setProductImg(productImg.getUrl());
            shoppingCart.setSkuName(productSku.getSkuName());
            shoppingCart.setOriginalPrice(productSku.getOriginalPrice());
            shoppingCart.setSellPrice(productSku.getSellPrice());
            shoppingCart.setStock(productSku.getStock());
        }
    }
}
